package com.example.imoocmall.controller;

import com.example.imoocmall.common.ApiRestResponse;
import com.example.imoocmall.filter.UserFilter;
import com.example.imoocmall.model.dao.CartMapper;
import com.example.imoocmall.model.vo.CartVo;
import com.example.imoocmall.service.impl.CartServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车controller
 */
@RestController
@Api(tags = "购物车")
@RequestMapping("/cart")
public class CartController {

    @Autowired
    CartServiceImpl cartService;

    @GetMapping("/list")
    @ApiOperation("/购物车列表展示")
    public ApiRestResponse list(){
        //内部获取用户id,防止横向越权
        List<CartVo> list = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(list);
    }

    @PostMapping("/add")
    @ApiOperation("添加商品到购物车")
    public ApiRestResponse add(@RequestParam Integer productId, @RequestParam Integer count){

        List<CartVo> cartVoList = cartService.add(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(cartVoList);
    }
    @PostMapping("/update")
    @ApiOperation("更新商品到购物车")
    public ApiRestResponse update(@RequestParam Integer productId, @RequestParam Integer count){

        List<CartVo> cartVoList = cartService.update(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(cartVoList);
    }


    @PostMapping("/delete")
    @ApiOperation("删除商品到购物车")
    public ApiRestResponse delete(@RequestParam Integer productId){
        //不能传入userID,cartID,否则可以删除别的购物车

        List<CartVo> cartVoList = cartService.delete(UserFilter.currentUser.getId(),productId);
        return ApiRestResponse.success(cartVoList);
    }

    @PostMapping("/select")
    @ApiOperation("选中/不选中购物车中的商品")
    public ApiRestResponse select(@RequestParam Integer productId,@RequestParam Integer selected){
        List<CartVo> cartVoList = cartService.selectOrNot(UserFilter.currentUser.getId(),productId,selected);
        return ApiRestResponse.success(cartVoList);
    }

    @PostMapping("/selectall")
    @ApiOperation("全选中/不选中购物车中的商品")
    public ApiRestResponse selectall(@RequestParam Integer selected){
        List<CartVo> cartVoList = cartService.selectOrNotAll(UserFilter.currentUser.getId(),selected);
        return ApiRestResponse.success(cartVoList);
    }

}

















